<template>
  <div class="wrap">
    <div class="title">
      <img src="../../images/main/server.png" alt>
      <span class="back" @click="backServer()">服务商管理</span>
      <img class="right" src="../../images/main/right.png" alt>
      <span>收款账号</span>
    </div>
    <div class="clear"></div>
    <div class="body">
      <div class="body-box">
        <form>
        <div class="list firlist">
          <img class="red" src="../../images/main/red.png" alt>
          <span class="txt">姓名：</span>
          <input type="text" name="accountName" v-validate="'required'"  v-model="addAccount.accountName" maxlength="10"  class="input" placeholder="请输入姓名">
           <span v-show="errors.has('accountName')" class="tips">请输入姓名</span>
          <!-- <span class="tips" v-if="!addAccount.accountName">请输入姓名</span> -->

        </div>
        <div class="list">
          <img class="red" src="../../images/main/red.png" alt>
          <span class="txt">银行名称：</span>
          <input type="text"  name="bankName" v-validate="'required'"   v-model="addAccount.bankName" maxlength="30" class="input" placeholder="请输入银行名称">
          <span v-show="errors.has('bankName')" class="tips">请输入银行名称</span>
          <!-- <span class="tips" v-if="!addAccount.bankName">请输入银行名称</span> -->
        </div>
        <div class="list">
          <img class="red" src="../../images/main/red.png" alt>
          <span class="txt">银行账号：</span>
          <input name="bankAccount" v-validate="'required'"  v-model="addAccount.bankAccount" maxlength="20" @input="bankAccountInp" type="text" class="input" placeholder="请输入银行账号">
          <span v-show="errors.has('bankAccount')" class="tips">请输入银行账号</span>
          <!-- <span class="tips" v-if="!addAccount.bankAccount">请输入银行账号</span> -->
        </div>

        <div class="submit" @click="addAccount_submit"><span v-if="isEdit">修改</span><span v-else>提交</span></div>
        </form>
      </div>
    </div>
  </div>
</template>
<script>
import axios from 'axios'
import qs from 'qs'
export default {
  created () {
    // 获取详情
    axios({
      method:'post',
      url:`${this.$store.state.url}/serviceAccount/selectDateils`,
      data:qs.stringify({
        serviceId: this.$route.query.id
      })
    }).then(res => {
      if (res.data.code == 0) {
        this.addAccount = res.data.data
        this.type = 'modify';
        if(this.addAccount.accountName&&this.addAccount.bankAccount&&this.addAccount.bankName){
          this.isEdit = true;
        }
      }
    });
  },
  data () {
    return {
      isEdit: false,
      type: 'add',
      addAccount: {
        accountName: '',
        bankName: '',
        bankAccount: '',
        serviceId: this.$route.query.id
      }
    }
  },
  methods: {
    bankAccountInp() {
      this.addAccount.bankAccount = isNaN ( this.addAccount.bankAccount ) ? 0 : this.addAccount.bankAccount.replace ( /\.|-/img, '' );
    },
    backServer: function () {
      this.$router.push({
        path: 'server'
      })
    },

    // 新增账户
    addAccount_submit() {
      var that = this
      this.$validator.validate().then(result => {
        if (!result) {
          // do stuff if not valid.
        } else {
          let obj = {};
          for ( let i in this.addAccount ) {
            obj[i] = this.addAccount[i];
          }
          if ( this.type == 'modify' ) {
            delete obj.serviceId;
          }
          obj.isEnterprise = false
          let _this = this;
          let url = this.type == 'add' ? `${this.$store.state.url}/serviceAccount/addServiceAccount` : `${this.$store.state.url}/tPartnerAccount/updateAccount`;
          axios.post (
            url,
            qs.stringify ( obj )
          ).then ( response => {
            let res = response.data;
            if ( res.code == 0 ) {
              this.$store.state.fn.success( this.type == 'add' ? '增加成功' : '修改成功' ).then (() => {
                _this.$router.push ({
                  path: '/main/server'
                });
                let logData = {
                loginTime:this.$cookie.get("loginTime"),
                account:this.$cookie.get("userAccount"),
                content: this.type == 'add'?'增加服务商收款账户':'修改服务商收款账户',
              operationTime:this.setDate(new Date()) + ' ' +this.setTime(new Date())
              }
              axios({
          method: "post",
          url: `${that.$store.state.url}/log/addLog`,
          data: qs.stringify(logData)
        }).then( (res) => {
          console.log(res,'222')
        })
              });
            } else {
              this.$alert ( '增加失败' );
            }
          }, () => {
            this.$alert ( '增加失败' );
          });
            }
        })
    },
     /**
     * 根据时间毫秒数 返回年月日
     */
    setDate: function(str) {
      let times = new Date(str);
      return `${times.getFullYear()}-${
        times.getMonth() + 1 < 10
          ? "0" + (times.getMonth() + 1)
          : times.getMonth() + 1
      }-${times.getDate() < 10 ? "0" + times.getDate() : times.getDate()}`;
    },
    /**
     * 根据时间毫秒数 返回时分秒
     */
    setTime: function(str) {
      let times = new Date(str);
      return `${
        times.getHours() < 10 ? "0" + times.getHours() : times.getHours()
      }:${
        times.getMinutes() < 10 ? "0" + times.getMinutes() : times.getMinutes()
      }:${
        times.getSeconds() < 10 ? "0" + times.getSeconds() : times.getSeconds()
      }`;
    }
  }
};
</script>
<style scoped>
.wrap {
  /* padding: 68px 136px;
  width: 100%; */
  width: 90%;
  margin: 40px auto;
}
.title {
  height: 33px;
  line-height: 33px;
  font-size: 24px;
  font-family: PingFangSC-Medium;
  font-weight: bold;
}
.title span {
  float: left;
}
.title img {
  width: 28px;
  height: 26px;
  float: left;
  padding-top: 5px;
  margin-right: 10px;
}
.title .right {
  width: 11px;
  height: 18px;
  padding-top: 9px;
  margin-left: 5px;
  margin-right: 6px;
}
.body {
  background: #ffffff;
  box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  /* width: 1490px; */
  height:500px;
  /* margin-top: 70px; */
  /* margin-left: 48px; */
  /* padding-left: 349px; */
  /* padding-top: 108px; */
  box-sizing: border-box;
}
.body-box{
  width: 700px;
  margin: 60px auto;
  padding:40px;
}
.list {
  height: 40px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #000000;
  line-height: 40px;
  margin-top: 47px;
}
.firlist {
  margin-top: 0;
}
.red {
  width: 10px;
  height: 10px;
}
.txt {
  width: 101px;
  display: inline-block;
  text-align: left;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  font-weight: bold;
  color: #000000;
}
.input {
  background: #ffffff;
  box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
  width: 502px;
  height: 40px;
  margin-left: 20px;
  padding-left: 10px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #888888;
  box-sizing: border-box;
  text-align: center;
  border-width: 1px;
}
.clear {
  clear: both;
}

.submit {
  background: #085dab;
  width: 255px;
  height: 55px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  line-height: 55px;
  text-align: center;
  /* margin-top: 240px;
  margin-left: 269px; */
  margin: 100px auto 0;
  cursor: pointer;
}
.back {
  cursor: pointer;
}
.tips {
  font-family: PingFangSC-Regular;
  font-size: 14px;
  /*color: #727272;*/
  color: red;
  margin-left: 35px;
  display: block;
  margin-left: 150px;
}
</style>
